﻿using UnityEngine;
using UnityEngine.UI;

public class UGUIRenderer : Image
{
    public Color _TintColor = Color.white;

    public Texture _MainTex = null;
    public Vector2 _MainTex_Scale = Vector2.one;
    public Vector2 _MainTex_Offset = Vector2.zero;

    public Texture _Mask = null;
    public Vector2 _Mask_Scale = Vector2.one;
    public Vector2 _Mask_Offset = Vector2.zero;

    public float _HeatTime = 0;
    public float _ForceX = 0;
    public float _ForceY = 0;

    public int _RenderQueue = 0;

    Material sharedMaterial = null;
    public Material orginMaterial { get { return base.material; } }

    public override Material material
    {
        get
        {
            if (sharedMaterial == null && base.material != null)
            {
                sharedMaterial = Instantiate(base.material);
            }
            return sharedMaterial;
        }

        set
        {
            if (base.material != value)
            {
                DestroyImmediate(sharedMaterial);
                sharedMaterial = null;
                ClearProperty();

                base.material = value;

                if (base.material != null)
                {
                    sharedMaterial = Instantiate(base.material);
                    SetMaterialDirty();
                    SetProperty();
                }
            }
        }
    }

    void Update()
    {
        UpdateProperty();
    }

    void UpdateProperty()
    {
        if (sharedMaterial != null)
        {
            if (sharedMaterial.HasProperty("_TintColor"))
            {
                sharedMaterial.SetColor("_TintColor", _TintColor);
            }

            if (sharedMaterial.HasProperty("_MainTex"))
            {
                sharedMaterial.SetTexture("_MainTex", _MainTex);
                sharedMaterial.SetTextureScale("_MainTex", _MainTex_Scale);
                sharedMaterial.SetTextureOffset("_MainTex", _MainTex_Offset);
            }

            if (sharedMaterial.HasProperty("_Mask"))
            {
                sharedMaterial.SetTexture("_Mask", _Mask);
                sharedMaterial.SetTextureScale("_Mask", _Mask_Scale);
                sharedMaterial.SetTextureOffset("_Mask", _Mask_Offset);
            }

            if (sharedMaterial.HasProperty("_HeatTime"))
            {
                sharedMaterial.SetFloat("_HeatTime", _HeatTime);
            }

            if (sharedMaterial.HasProperty("_ForceX"))
            {
                sharedMaterial.SetFloat("_ForceX", _ForceX);
            }

            if (sharedMaterial.HasProperty("_ForceY"))
            {
                sharedMaterial.SetFloat("_ForceY", _ForceY);
            }

            sharedMaterial.renderQueue = _RenderQueue;

            SetMaterialDirty();
        }
    }

    void SetProperty()
    {
        if (base.material != null)
        {
            if (base.material.HasProperty("_TintColor"))
            {
                _TintColor = base.material.GetColor("_TintColor");
            }

            if (base.material.HasProperty("_MainTex"))
            {
                _MainTex = base.material.GetTexture("_MainTex");
                _MainTex_Scale = base.material.GetTextureScale("_MainTex");
                _MainTex_Offset = base.material.GetTextureOffset("_MainTex");
            }

            if (base.material.HasProperty("_Mask"))
            {
                _Mask = base.material.GetTexture("_Mask");
                _Mask_Scale = base.material.GetTextureScale("_Mask");
                _Mask_Offset = base.material.GetTextureOffset("_Mask");
            }

            if (base.material.HasProperty("_HeatTime"))
            {
                _HeatTime = base.material.GetFloat("_HeatTime");
            }

            if (base.material.HasProperty("_ForceX"))
            {
                _ForceX = base.material.GetFloat("_ForceX");
            }

            if (base.material.HasProperty("_ForceY"))
            {
                _ForceY = base.material.GetFloat("_ForceY");
            }

            _RenderQueue = base.material.renderQueue;
        }
    }

    void ClearProperty()
    {
        _TintColor = Color.white;

        _MainTex = null;
        _MainTex_Scale = Vector2.one;
        _MainTex_Offset = Vector2.zero;

        _Mask = null;        
        _Mask_Scale = Vector2.one;
        _Mask_Offset = Vector2.zero;

        _HeatTime = 0;
        _ForceX = 0;
        _ForceY = 0;

        _RenderQueue = 0;
    }
}